package com.bank.ui.customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class TransferPanel extends JPanel {
    private JTextField fromCardIDField;
    private JTextField toCardIDField;
    private JTextField amountField;
    private JButton transferButton;
    private JLabel messageLabel;
    private List<TransferListener> transferListeners = new ArrayList<>();

    public TransferPanel() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 转出卡号输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("转出卡号:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        fromCardIDField = new JTextField(16);
        add(fromCardIDField, gbc);

        // 转入卡号输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("转入卡号:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        toCardIDField = new JTextField(16);
        add(toCardIDField, gbc);

        // 金额输入框
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("金额:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        amountField = new JTextField(10);
        add(amountField, gbc);

        // 转账按钮
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        transferButton = new JButton("转账");
        add(transferButton, gbc);

        // 消息标签
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        messageLabel = new JLabel("", JLabel.CENTER);
        add(messageLabel, gbc);

        // 添加事件监听器
        transferButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performTransfer();
            }
        });
    }

    // 执行转账操作
    private void performTransfer() {
        String fromCardID = fromCardIDField.getText();
        String toCardID = toCardIDField.getText();
        String amountStr = amountField.getText();

        // 验证输入
        if (fromCardID.isEmpty() || toCardID.isEmpty() || amountStr.isEmpty()) {
            messageLabel.setText("转出卡号、转入卡号和金额不能为空！");
            messageLabel.setForeground(Color.RED);
            return;
        }

        try {
            double amount = Double.parseDouble(amountStr);
            if (amount <= 0) {
                messageLabel.setText("金额必须大于0！");
                messageLabel.setForeground(Color.RED);
                return;
            }

            // 清除之前的错误消息
            messageLabel.setText("");

            // 触发转账事件
            fireTransferEvent(new TransferEvent(this, fromCardID, toCardID, amount));

        } catch (NumberFormatException e) {
            messageLabel.setText("请输入有效的金额！");
            messageLabel.setForeground(Color.RED);
        }
    }

    // 添加转账监听器
    public void addTransferListener(TransferListener listener) {
        transferListeners.add(listener);
    }

    // 移除转账监听器
    public void removeTransferListener(TransferListener listener) {
        transferListeners.remove(listener);
    }

    // 触发转账事件
    private void fireTransferEvent(TransferEvent event) {
        for (TransferListener listener : transferListeners) {
            listener.transferRequested(event);
        }
    }

    // 转账事件类
    public static class TransferEvent {
        private Object source;
        private String fromCardID;
        private String toCardID;
        private double amount;

        public TransferEvent(Object source, String fromCardID, String toCardID, double amount) {
            this.source = source;
            this.fromCardID = fromCardID;
            this.toCardID = toCardID;
            this.amount = amount;
        }

        public Object getSource() {
            return source;
        }

        public String getFromCardID() {
            return fromCardID;
        }

        public String getToCardID() {
            return toCardID;
        }

        public double getAmount() {
            return amount;
        }
    }

    // 转账监听器接口
    public interface TransferListener {
        void transferRequested(TransferEvent event);
    }
}